package com.gjy.netty.ag.bio;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-05-17 16:58:06
 */
public class TimeServer {

    private static final Logger logger = LoggerFactory.getLogger(TimeServer.class);
    private static Integer PORT = 8000;

    public static void main(String[] args) {

        if (args != null && args.length > 0) {
            try {
                PORT = Integer.valueOf(args[0]);
            } catch (Exception e) {
                PORT = 8000;
            }
        }

        int processors = Runtime.getRuntime().availableProcessors();
        ExecutorService pool = Executors.newFixedThreadPool(processors);

        try (ServerSocket server = new ServerSocket(PORT)) {
            logger.info("time server is start int port = {}", PORT);
            while (true) {
                Socket socket = server.accept();
                pool.submit(new TimeServerHandler(socket));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
